<?php
// Include class file that performs all the work.
include('occam.php');
include('config.php');
//  Assign variables passed from GET
//  This needs to be first because coblade depends on these variables
$ip=$_GET['ip'];
$port=$_GET['port'];
$cmd=$_GET['cmd'];

//  Declare colors based on severity
$crit_color = "#ff5555";
$major_color = "#ffab57";
$minor_color = "#ffff57";
$pass_color = "#57ff57";

// Create a new object to connect to the blade and perform caommands against
$coblade = &New occam($ip,"occam","razor","100/1");

// Open the session to the blade
$coblade->opentelnetsession();

//  TESTING DELETE AFTER
/*$modem_status = 1;
$modem_dspeed = 16252;
$modem_uspeed = 768;
$modem_down_snr = 7.6;
$modem_up_snr = 20.0;
$modem_retrains = 20;
$modem_uptime = "0d0h10m";
*/
//  END OF TESTING, DELETE AREA ABOVE

###----  START PARSING show_dsl_service
$coblade->show_dsl_service($port,"1");
$sh_dsl_service_output = $coblade->getoutput();
//echo $output;
// This section parses the output for the access-profile and admin-status
// from the "sh dsl service int atm $port" command
preg_match("/0\/35(.*)/", $sh_dsl_service_output, $internet_matches);
preg_match("/0\/36(.*)/", $sh_dsl_service_output, $video_matches);

$internet_data = $internet_matches[1];
$internet_data = preg_replace("/\s\s+/", ",", $internet_data);
$video_data = $video_matches[1];
$video_data = preg_replace("/\s\s+/", ",", $video_data);

list( ,$internet_encapsulation, $internet_access_profile, $internet_admin_status) = explode(",", $internet_data);
list( ,$video_encapsulation, $video_access_profile, $video_admin_status) = explode(",", $video_data);
###----  END OF PARSING show_dsl_service

###----  START PARSING show_dsl_sum
$coblade->show_dsl_sum($port,"1");
$dsl_sum_output = $coblade->getoutput();
//echo $output;
preg_match("/$port(.*)\|(.*)/", $dsl_sum_output, $dsl_sum_matches);
list( ,$modem_dspeed ,$modem_uspeed ,$modem_down_snr ,$modem_up_snr , , ,$modem_retrains, $modem_uptime) = explode("|", $dsl_sum_matches[0]);
###---- END OF PARSING show_dsl_sum

###---  START PARSING show_dsl_performance
$coblade->port_performance($port,"dsl","1");
$dsl_perf_output = $coblade->getoutput();
//echo $dsl_perf_output;
preg_match_all("/CV(.*)/", $dsl_perf_output, $cv_matches);
preg_match_all("/EC\ :(.*)/", $dsl_perf_output, $ec_matches);
$current_cv_matches = preg_replace("/\s\s+/", ",", $cv_matches[0][0]);
$current_ec_matches = preg_replace("/\s\s+/", ",", $ec_matches[0][0]);
$previous_cv_matches = preg_replace("/\s\s+/", ",", $cv_matches[0][1]);
$previous_ec_matches = preg_replace("/\s\s+/", ",", $ec_matches[0][1]);
$current24_cv_matches = preg_replace("/\s\s+/", ",", $cv_matches[0][2]);
$current24_ec_matches = preg_replace("/\s\s+/", ",", $ec_matches[0][2]);
$previous24_cv_matches = preg_replace("/\s\s+/", ",", $cv_matches[0][3]);
$previous24_ec_matches = preg_replace("/\s\s+/", ",", $ec_matches[0][3]);
list( , ,$current_near_cv , ,$current_far_cv) = explode(",", $current_cv_matches);
list( , ,$current_near_ec , ,$current_far_ec) = explode(",", $current_ec_matches);
list( , ,$previous_near_cv , ,$previous_far_cv) = explode(",", $previous_cv_matches);
list( , ,$previous_near_ec , ,$previous_far_ec) = explode(",", $previous_ec_matches);
list( , ,$current24_near_cv , ,$current24_far_cv) = explode(",", $current24_cv_matches);
list( , ,$current24_near_ec , ,$current24_far_ec) = explode(",", $current24_ec_matches);
list( , ,$previous24_near_cv , ,$previous24_far_cv) = explode(",", $previous24_cv_matches);
list( , ,$previous24_near_ec , ,$previous24_far_ec) = explode(",", $previous24_ec_matches);
###---  END OF PARSING show_dsl_performance

###---  Start PARSING show_dhcp_clients
$coblade->show_dhcp_clients($port,"dsl","1");
$dhcp_clients_output = $coblade->getoutput();
preg_match_all("/(.*)\s..:..:..:..:..:..\s(.*)\.(.*)\.(.*)\.(.*)/", $dhcp_clients_output, $dhcp_matches);

//print_r($device_matches);
foreach ($dhcp_matches[0] as $val) {
	// Replace spaces in returned array with commas
        $val = preg_replace("/\s\s+/", ",", $val);
        // Seperate each field in the returned array into variables seperated by commas above
        list( $dhcp_vlan, $dhcp_mac_address, $dhcp_ip_address, $dhcp_state, $dhcp_persistent, $dhcp_weekday, $dhcp_month, $dhcp_date, $dhcp_time, $dhcp_year) = explode(",", $val);

        $dhcp_lease = $weekday . " " . $month . " " . $date . " " . $time . " " . $year;

	//$dhcp_array[] = "$dhcp_vlan,$dhcp_mac_address,$dhcp_ip_address";
	$dhcp_array[] = $val;
                /*if ($vlan != '3') {
                        $bg_color = "red";
                }
                echo "<tr style=\"background: $bg_color\">";
                echo "<td>$vlan</td><td>$ip_address</td><td>$lease</td><td>$persistent</td><td>$state</td><td>$mac_address</td><td>
                        <form id=\"delete_dhcp\">
                                <input type=\"button\" value=\"delete\" onClick=\"delete_dhcp('$mac_address')\"/>
                        </form>
                        </td>";
                echo "</tr>";*/
}
###---  END OF PARSING show_dhcp_clients

###---  Start PARSING show_multicast_info
$coblade->show_multicast_info($port,"dsl","1");
$show_multicast_output = $coblade->getoutput();

preg_match_all("/Group:(.*)/", $show_multicast_output, $multicast_group_matches);

foreach ($multicast_group_matches[0] as $val) {
	$val = preg_replace("/\s\s+/", ",", $val);
	$group_array[] = $val;
}

###---  END OF PARSING show_multicast_info

###-----------------------------------------------###
###   Format the layout of the content below      ###
###-----------------------------------------------###


//echo $modem_uptime;
echo "Modem State: ";
// If the modems speed is 0, then it is down
if ($modem_dspeed != 0) {
	echo "<span style=\"background: $pass_color\">up</span><br />\n";
}
else {
	echo "<span style=\"background: $crit_color\">down</span><br/>\n";
	$troubleshoot[] = "Make sure that the modem has power";
	$troubleshoot[] = "Verify phone line is plugged in to modem";
	$troubleshoot[] = "Verify correct phone line is plugged in to modem";
	$troubleshoot[] = "Verify phone line going into modem does NOT have a filter on it";
}

//    What speed is the modem sync'd up at?
//TODO:  Do a comparison to the dsl-profile attached to port
echo "DS Rate: ";
// Is the modem sync'd at a TV3 speed?
if ($modem_dspeed >= 16250) {
	echo "<span style=\"background: $pass_color\">$modem_dspeed</span><br/>\n";
}
// Is the modem sync'd at a TV2 speed?
else if ($modem_dspeed >= 11450 AND $modem_dspeed <= 11456) {
	echo "<span style=\"background: $pass_color\">$modem_dspeed</span><br/>\n";
}
// Is the modem sync'd at a TV1 speed?
else if ($modem_dspeed >= 6650 AND $modem_dspeed <= 6700) {
	echo "<span style=\"background: $pass_color\">$modem_dspeed</span><br/>\n";
}
// Is the modem sync'd at a 2M data OR 3M data speed?
else if ($modem_dspeed >= 2040 AND $modem_speed <= 2050 OR $modem_dspeed >= 3070 AND $modem_dspeed <= 3080){
	echo "<span style=\"background: $pass_color\">$modem_dspeed</span><br/>\n";
}
// If the modem does not sync at the above speeds, consider it to be critical
else{
	echo "<span style=\"background: $crit_color\">$modem_dspeed</span><br/>\n";
	$trouble[] = "Try rebooting the modem and check speed again";
}
//TODO:  Do a comparison to the dsl-profile attached to port
echo "US Rate: $modem_uspeed<br/>\n";

//      What are the SNR margins at?
echo "DS SnR: ";
// If the snr margin is greater then or equal to 8db it is ok
if ($modem_down_snr >= 8){
	echo "<span style=\"background: $pass_color\">$modem_down_snr</span><br/>\n";
}
// If the snr margin is lower then 8db, but greater then 6db flag as minor alarm
else if ($modem_down_snr < 8 AND $modem_down_snr > 6){
	echo "<span style=\"background: $minor_color\">$modem_down_snr</span><br/>\n";
	$trouble[] = "Try rebooting the modem and check snr again";
}
// If the snr margin is lower then 6db, then flag as critical
else{
	echo "<span style=\"background: $crit_color\">$modem_down_snr</span><br/>\n";
	$trouble[] = "Try rebooting the modem and check snr again";
}

echo "US SnR: ";
// If the snr margin is greater then or equal to 8db it is ok
if ($modem_up_snr >= 8){
	echo "<span style=\"background: $pass_color\">$modem_up_snr</span><br/>\n";
}
// If the snr margin is lower then 8db, but greater then 6db flag as minor alarm
else if ($modem_up_snr < 8 AND $modem_up_snr > 6){
	echo "<span style=\"background: $minor_color\">$modem_up_snr</span><br/>\n";
	$trouble[] = "Try rebooting the modem and check snr again";
}
// If the snr margin is lower then 6db, then flag as critical
else{
	echo "<span style=\"background: $crit_color\">$modem_up_snr</span><br/>\n";
	$trouble[] = "Try rebooting the modem and check snr again";
}

//  Is internet service turned on
echo "Internet State: ";
if ($internet_admin_status == "up") {
	echo "<span style=\"background: $pass_color\">on</span><br/>\n";
}
else {
	echo "<span style=\"background: $crit_color\">off</span><br/>\n";
	$trouble[] = "Does customer have internet service? If so, restore it.";
}

//  Is video service turned on
echo "Video State: ";
if ($video_admin_status == "up") {
	//If video is on, do STB's have IP's
	echo "<span style=\"background: $pass_color\">on</span><br/>\n";
	#$coblade->show_dhcp_clients($port,"dsl","1");
	#$dhcp_output = $coblade->getoutput();
	#echo $dhcp_output;
		//If STB's have IP's are they joined to multicast streams
}
else{
	echo "<span style=\"background: $crit_color\">off</span><br/>\n";
	$trouble[] = "Does customer have video service?  If so, restore it.";
}
//  How many retrains has the port had
echo "Retrains: $modem_retrains<br/>\n";

//    How long has the modem been up recently?
echo "Uptime: $modem_uptime<br/>\n";

###---  START of performance table
//  How many errors is the port incurring in the current 15 minutes?
echo "<br/>";
echo "Line Performance:<br/>\n";
echo "<table>";
echo "<tr><th></th><th width='100px'>Near End:</th><th width='100px'>Far End:</th></tr>";
echo "<tr><td>current:</td>";
if ($current_near_cv > 0) {
	echo "<td style=\"background: $crit_color\">$current_near_cv CV</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$current_near_cv CV</td>";
}
if ($current_far_cv > 0) {
	echo "<td style=\"background: $crit_color\">$current_far_cv CV</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else {
	echo "<td style=\"background: $pass_color\">$current_far_cv CV</td>";
}
echo "</tr>";
echo "<tr><td></td>";
if ($current_near_ec > 0 AND $current_near_ec < 500 ) {
	echo "<td style=\"background: $minor_color\">$current_near_ec EC</td>";
}
else if ($current_near_ec > 500 AND $current_near_ec < 2000 ) {
	echo "<td style=\"background: $major_color\">$current_near_ec EC</td>";
}
else if ($current_near_ec > 2000) {
	echo "<td style=\"background: $crit_color\">$current_near_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$current_near_ec EC</td>";
}
if ($current_far_ec > 0 AND $current_far_ec < 500 ) {
	echo "<td style=\"background: $minor_color\">$current_far_ec EC</td>";
}
else if ($current_far_ec > 500 AND $current_far_ec < 2000 ) {
	echo "<td style=\"background: $major_color\">$current_far_ec EC</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else if ($current_far_ec > 2000) {
	echo "<td style=\"background: $crit_color\">$current_far_ec EC</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else {
	echo "<td style=\"background: $pass_color\">$current_far_ec EC</td>";
}
echo "</tr>";
//    In the past 15 minutes?
echo "<tr><td>previous:</td>";
if ($previous_near_cv > 0) {
	echo "<td style=\"background: $crit_color\">$previous_near_cv CV</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous_near_cv CV</td>";
}
if ($previous_far_cv > 0) {
	echo "<td style=\"background: $crit_color\">$previous_far_cv CV</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else {
	echo "<td style=\"background: $pass_color\">$previous_far_cv CV</td>";
}
echo "</tr>";
echo "<tr><td></td>";
if ($previous_near_ec > 0 AND $previous_near_ec < 500) {
	echo "<td style=\"background: $minor_color\">$previous_near_ec EC</td>";
}
else if ($previous_near_ec > 500 AND $previous_near_ec < 2000) {
	echo "<td style=\"background: $major_color\">$previous_near_ec EC</td>";
}
else if ($previous_near_ec > 2000) {
	echo "<td style=\"background: $crit_color\">$previous_near_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous_near_ec EC</td>";
}
if ($previous_far_ec > 0 AND $previous_far_ec < 500) {
	echo "<td style=\"background: $minor_color\">$previous_far_ec EC</td>";
}
if ($previous_far_ec > 500 AND $previous_far_ec < 2000) {
	echo "<td style=\"background: $major_color\">$previous_far_ec EC</td>";
}
if ($previous_far_ec > 2000) {
	echo "<td style=\"background: $crit_color\">$previous_far_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous_far_ec EC</td>";
}
echo "</tr>";
//      In the current 24hrs?
echo "<tr><td>current 24:</td>";
if ($current24_near_cv > 0) {
	echo "<td style=\"background: $crit_color\">$current24_near_cv CV</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$current24_near_cv CV</td>";
}
if ($current24_far_cv > 0) {
	echo "<td style=\"background: $crit_color\">$current24_far_cv CV</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else {
	echo "<td style=\"background: $pass_color\">$current24_far_cv CV</td>";
}
echo "</tr>";
echo "<tr><td></td>";
if ($current24_near_ec > 0 AND $current24_near_ec < 2000) {
	echo "<td style=\"background: $minor_color\">$current24_near_ec EC</td>";
}
else if ($current24_near_ec > 2000 AND $current24_near_ec < 10000) {
	echo "<td style=\"background: $major_color\">$current24_near_ec EC</td>";
}
else if ($current24_near_ec > 10000) {
	echo "<td style=\"background: $crit_color\">$current24_near_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$current24_near_ec EC</td>";
}
if ($current24_far_ec > 0 AND $current24_far_ec < 2000) {
	echo "<td style=\"background: $minor_color\">$current24_far_ec EC</td>";
}
else if ($current24_far_ec > 2000 AND $current24_far_ec < 10000) {
	echo "<td style=\"background: $major_color\">$current24_far_ec EC</td>";
}
else if ($current24_far_ec > 10000) {
	echo "<td style=\"background: $crit_color\">$current24_far_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$current24_far_ec EC</td>";
}
echo "</tr>";
//        In the past 24hrs?
echo "<tr><td>previous 24:</td>";
if ($previous24_near_cv > 0) {
	echo "<td style=\"background: $crit_color\">$previous24_near_cv CV</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous24_near_cv CV</td>";
}
if ($previous24_far_cv > 0) {
	echo "<td style=\"background: $crit_color\">$previous24_far_cv CV</td>";
	$trouble[] = "Has the customer added an unfiltered phone to the line? If so, remove it.";
	$trouble[] = "Has the customer done anything with their phone lines in their house?  If so, create a trouble ticket.";
}
else {
	echo "<td style=\"background: $pass_color\">$previous24_far_cv CV</td>";
}
echo "</tr>";
echo "<tr><td></td>";
if ($previous24_near_ec > 0 AND $previous24_near_ec < 2000) {
	echo "<td style=\"background: $minor_color\">$previous24_near_ec EC</td>";
}
else if ($previous24_near_ec > 2000 AND $previous24_near_ec < 10000) {
	echo "<td style=\"background: $major_color\">$previous24_near_ec EC</td>";
}
else if ($previous24_near_ec > 10000) {
	echo "<td style=\"background: $crit_color\">$previous24_near_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous24_near_ec EC</td>";
}
if ($previous24_far_ec > 0 AND $previous24_far_ec < 2000) {
	echo "<td style=\"background: $minor_color\">$previous24_far_ec EC</td>";
}
else if ($previous24_far_ec > 2000 AND $previous24_far_ec < 10000) {
	echo "<td style=\"background: $major_color\">$previous24_far_ec EC</td>";
}
else if ($previous24_far_ec > 10000) {
	echo "<td style=\"background: $crit_color\">$previous24_far_ec EC</td>";
}
else {
	echo "<td style=\"background: $pass_color\">$previous24_far_ec EC</td>";
}
echo "</tr>";
echo "</table>";

###---  END of performance table

echo "<input type='button' id='shutnoshut' value='Reset Port' onClick=\"shut_noshut()\"/>";

        echo "<script type=\"text/javascript\">
        function shut_noshut() {
                MochaUI.notification('Resetting $ip/$port');
                var url='pages/blade_info.php';
                url=url+'?ip=$ip';
                url=url+'&port=$port';
                url=url+'&cmd=shut_no_shut';
                url=url+'&nocache='+Math.random();
                MochaUI.updateContent({
                        'element': $('panel1'),
                        'loadMethod': 'xhr',
                        'url': url,
                        'title': 'Results',
                        'padding': { top: 8, right: 8, bottom: 8, left: 8 }
                });
        };
        </script>";


/*
###---  START of stb, multicast, dhcp information
echo "<table>";
echo "<tr><td>This section will contain the DHCP/Multicast/STB information<br/>";
foreach ($dhcp_array as $val) {
	echo "$val<br/>";
}
foreach ($group_array as $val) {
	echo "$val<br/>";
}
echo "$show_multicast_output</div>";
###---  END of stb, multicast, dhcp information
*/

###---  LIST TROUBLESHOOTING TIPS HERE
if (!empty($troubleshoot)){
	echo "<br/><br/>";
	echo "<div width=\"100%\" style=\"border: solid 1px #3d3d3d; padding-left: 8px\">";
	echo "Troubleshooting Steps:<br/>";
	echo "<ul>";
	foreach ($troubleshoot as $item) {
		// Checkboxes are only meant to help CSR keep track of what has been done
		echo "<input type=\"checkbox\" />$item<br/>\n";
	}
	echo "</ul>";
	echo "</div>";
}
###---  END OF TROUBLESHOOTING TIPS

?>
